package pool;

import java.util.concurrent.*;

public class Demo2 {
    public static void main(String[] args) {
        //阻塞队列
        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(5);

        //获取CPU核数
        int i = Runtime.getRuntime().availableProcessors();

        //自定义线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,
                5,
                3,
                TimeUnit.SECONDS,
                workQueue,
                Executors.defaultThreadFactory(),
                //new ThreadPoolExecutor.AbortPolicy()        抛出异常
                //new ThreadPoolExecutor.CallerRunsPolicy()  //哪来的去哪里
                //new ThreadPoolExecutor.DiscardPolicy()     //不会抛出异常 会放弃任务
                new ThreadPoolExecutor.DiscardOldestPolicy() //尝试和最早的竞争

        );
        //使用线程池
        try {
            //最大承载= workQueue + maximumPoolSize
            for (int i1 = 0; i1 < 15; i1++) {
                threadPoolExecutor.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" 线程创建成功！");
                });
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPoolExecutor.shutdown();
        }
    }
}
